package com.tensquare.article.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.dao.ArticleDao;
import com.tensquare.article.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.print.PageableDoc;
import util.IdWorker;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class ArticleService {
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private IdWorker idWorker;
    public List<Article> findAll() {
        return articleDao.selectList(null);
    }

    public Article findById(String articleId) {
        return articleDao.selectById(articleId);
    }

    public void sava(Article article) {
        String id = idWorker.nextId() + "";
        article.setId(id);
        article.setVisits(0); //浏览量
        article.setThumbup(0); //点赞数
        article.setComment(0);  //评论数

        //新增
        articleDao.insert(article);
    }

    public void updateById(Article article) {
        articleDao.updateById(article);

//        EntityWrapper<Article> wrapper = new EntityWrapper<>();
//        wrapper.eq("id",article.getId());
//
//        articleDao.update(article,wrapper);
    }

    public void deleteById(String articleId) {
        articleDao.deleteById(articleId);
    }

    public Page<Article> findByPage(Map<String, Object> map, Integer page, Integer size) {
        EntityWrapper<Article> wrapper = new EntityWrapper<>();
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
//            if(map.get(key) != null){
//                wrapper.eq(key,map.get(key));
//            }

            wrapper.eq(map.get(key) != null,key,map.get(key));

        }
        Page<Article> pageData = new Page<>(page,size);
        List<Article> list = articleDao.selectPage(pageData,wrapper);
        pageData.setRecords(list);
        return pageData;
    }
}
